package io.github.melin.sqlflow.parser.flink;

import io.github.melin.sqlflow.metadata.MetadataService;
import io.github.melin.sqlflow.metadata.QualifiedObjectName;
import io.github.melin.sqlflow.metadata.SchemaTable;
import io.github.melin.sqlflow.metadata.ViewDefinition;
import io.github.melin.sqlflow.tree.QualifiedName;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class JdbcQueryMetadataService implements MetadataService {
    private final List<SchemaTable> tables = new ArrayList();

    @Override
    public Optional<String> getSchema() {
        return Optional.empty();
    }

    @Override
    public Optional<String> getCatalog() {
        return Optional.empty();
    }

    @Override
    public boolean isAggregationFunction(QualifiedName name) {
        return false;
    }


    @Override
    public Optional<SchemaTable> getTableSchema(QualifiedObjectName targetTable) {
//        System.out.println("Searching for table: " + targetTable);
//        System.out.println("Available tables: " + tables);
//        Optional<SchemaTable> result = tables.stream()
//                .filter(table -> (table.getSchemaName() + "." + table.getTableName()).equals(targetTable.toString()))
//                .findFirst();
//        System.out.println("Result: " + result);
        return tables.stream()
                .filter(table -> (table.getSchemaName() + "." + table.getTableName()).equals(targetTable.toString()))
                .findFirst();
    }
    @Override
    public Optional<ViewDefinition> getView(QualifiedObjectName viewName) {
        return Optional.empty();
    }
    public MetadataService addTableMetadata(SchemaTable schemaTable) {
        this.tables.add(schemaTable);
        return this;
    }
    public MetadataService addTableMetadata(List<SchemaTable> schemaTables) {
        this.tables.addAll(schemaTables);
        return this;
    }
}
